<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/22
 * Time: 16:38
 */

namespace app\common\model;


use think\Cache;
use XznsTools\cache\CacheManage;

class ZlAstrExerciseType extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_astr_exercise_type';
    protected $cacheModel = 'astr_exercise_type';

    /**
     * 缓存获取所有考核类型信息
     * @return array
     * @throws \Exception
     */
    public function getExerciseTypeInfo()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->ZsGetIocData('list', function ()
            {
                return $this->getExerciseTypeIdFromDms();
            });

        if ($ids)
        {
            $data = $this->batchGetInfoByIds($ids, 'id,name,img,description,time,num');
        } else
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 数据库获取所有考核类型id
     * @return array
     */
    public function getExerciseTypeIdFromDms()
    {
        try
        {
            $ids = $this->where('status', 1)->order('sort DESC, id DESC')->column('id', 'id');
            $ids = array_flip_key_value($ids);
        }
        catch (\Exception $e)
        {
            $ids = [];
        }

        return $ids;
    }

    /**
     * 数据库批量获取考核类型信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        try
        {
            $data = $this->where($where)->order('sort DESC, id DESC')->column('id,name,img,description,time,num', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
